package com.healthtop.service.impl;

import com.healthtop.common.Specification;
import com.healthtop.dao.XsGameCategoryDao;
import com.healthtop.dao.XsGameDetailsDao;
import com.healthtop.dto.XsGameDetailsDto;
import com.healthtop.model.*;
import com.healthtop.service.*;
import com.healthtop.utils.AliyunOSSClientUtil;
import com.healthtop.utils.DateUtil;
import com.healthtop.utils.UserUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Service
public class XsGameDetailsServiceImpl implements XsGameDetailsService {

    @Autowired
    private XsGameDetailsDao xsGameDetailsDao;

    @Autowired
    private XsGameGategoryService xsGameGategoryService;

    @Autowired
    private XsDictionaryService xsDictionaryService;

    @Autowired
    private XsImgService xsImgService;

    @Autowired
    private XsArticleCategoryService xsArticleCategoryService;

    @Override
    public void delete(XsGameDetailsDto xsGameDetailsDto) {
        XsGameDetails xsGameDetails = new XsGameDetails();
        xsGameDetails.setDelTime(new Date());
        xsGameDetails.setDelFalg(xsGameDetailsDto.getDelFalg());
        xsGameDetails.setId(xsGameDetailsDto.getId());
        xsGameDetails.setDelUser(UserUtils.getCurrentUser().getUid());
        xsGameDetailsDao.update(xsGameDetails);
    }

    @Override
    public void operate(XsGameDetailsDto xsGameDetailsDto) {
        XsGameDetails xsGameDetails = new XsGameDetails();
        BeanUtils.copyProperties(xsGameDetailsDto,xsGameDetails);
        if(!ObjectUtils.isEmpty(xsGameDetailsDto.getImage())){
            try {
                String img = AliyunOSSClientUtil.upload(xsGameDetailsDto.getImage());
                XsImg xsImg = new XsImg();
                xsImg.setImgUrl("https://jianghexing-oss.oss-cn-beijing.aliyuncs.com/"+img);
                xsImg.setCreateTime(new Date());
                xsImg.setCreateUser(UserUtils.getCurrentUser().getUid());
                xsImg.setDelFalg("0");
                xsImgService.insert(xsImg);
                xsGameDetails.setGameDimageId(xsImg.getId());
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if(xsGameDetailsDto.getMode()== Specification.OperateMode.CREATE.index){
            Date date = new Date();
            xsGameDetails.setCreateTime(date);
            xsGameDetails.setCreateUser(UserUtils.getCurrentUser().getUid());
            xsGameDetails.setUpdateTime(date);
            xsGameDetails.setDelTime(date);
            xsGameDetailsDao.insert(xsGameDetails);
        }else if(xsGameDetailsDto.getMode()==Specification.OperateMode.UPDATE.index){
           if(ObjectUtils.isEmpty(xsGameDetailsDto.getImage())){
               xsGameDetails.setGameDimageId(null);
           }
            xsGameDetails.setUpdateTime(new Date());
            xsGameDetails.setUpdateUser(UserUtils.getCurrentUser().getUid());
            xsGameDetailsDao.update(xsGameDetails);
        }

    }

    @Override
    public XsGameDetailsDto forward(XsGameDetailsDto xsGameDetailsDto) {
        if(xsGameDetailsDto.getMode()== Specification.OperateMode.UPDATE.index||xsGameDetailsDto.getMode()==Specification.OperateMode.VIEW.index){
            XsGameDetails xsGameDetails = xsGameDetailsDao.selectById(xsGameDetailsDto.getId());
            BeanUtils.copyProperties(xsGameDetails,xsGameDetailsDto);
            if(xsGameDetailsDto.getGameDimageId()!=0) {
                xsGameDetailsDto.setImage(xsImgService.selectImgById(xsGameDetails.getGameDimageId()).getImgUrl());
            }
            List<XsArticleCategory> articleCategoryList = xsArticleCategoryService.selectByGameId(xsGameDetails.getGameId());
            xsGameDetailsDto.setArticleCategoryList(articleCategoryList);
        }
        List<XsGameCategory> gameCategoryList = xsGameGategoryService.selectAll();
        xsGameDetailsDto.setGameCategoryList(gameCategoryList);
//        List<XsDictionary> articleCategoryList =xsDictionaryService.selectByType(40L);

        return xsGameDetailsDto;
    }

    @Override
    public XsGameDetailsDto styleGuideDetail(XsGameDetailsDto xsGameDetailsDto) {
        XsGameDetails xsGameDetails = xsGameDetailsDao.selectById(xsGameDetailsDto.getId());
        BeanUtils.copyProperties(xsGameDetails,xsGameDetailsDto);
        xsGameDetailsDto.setCreateTime(DateUtil.dateFormat(xsGameDetails.getCreateTime(),DateUtil.defaultFullPattern));
        return xsGameDetailsDto;
    }
}
